'''
Created on Mar 29, 2014

@author: Luo
'''
import requests
import hashlib
from pytz import timezone
import datetime

#tinpyshop detail
# 2014-03-29 16:36:03.544 jfpal[14501:60b] delegate: <PaymentViewController: 0x19c53140> POST: https://mfront.jfpal.com:7555/unifiedAction.do, UserInfoQuery data: 
# 2014-03-29 16:36:03.735 jfpal[14501:60b] respData error: {
#     appUser = jfpal;
#     application = UserInfoQuery;
#     clientType = 04;
#     data = "{\"result\":{\"message\":\"\U6210\U529f\",\"resultCode\":\"0000\"},\"resultBean\":{\"customerId\":\"\",\"remark\":\"\",\"email\":\"\",\"certPid\":\"\",\"branchId\":\"\",\"authenFlag\":\"4\",\"realName\":\"\U7f57**\",\"attention\":\"\"}}";
#     dataType = json;
#     mobileSerialNum = 3745B2FAF677A344EB5290A89B2A777E00000000;
#     osType = "iOS7.1";
#     respCode = 0000;
#     respDesc = "\U6210\U529f\Uff01";
#     result =     {
#         message = "\U6210\U529f";
#         resultCode = 0000;
#     };
#     resultBean =     {
#         attention = "";
#         authenFlag = 4;
#         branchId = "";
#         certPid = "";
#         customerId = "";
#         email = "";
#         realName = "\U7f57**";
#         remark = "";
#     };
#     sign = C4678FBB2A7B4A7267DACF4E7B1B934B;
#     transDate = 20140329;
#     transLogNo = 000006;
#     transTime = 163603;
#     version = "2.5.3";
# }

#u'<?xml version="1.0" encoding="UTF-8"?><JFPay application="UserInfoQuery.Rsp" mobileSerialNum="B8:E8:56:41:84:FC" osType="iOS7.1" version="2.5.3" appUser="jfpal" clientType="04"><respCode>0000</respCode><data><![CDATA[{"result":{"message":"\u6210\u529f","resultCode":"0000"},"resultBean":{"customerId":"","remark":"","email":"","certPid":"","branchId":"","authenFlag":"3","realName":"\u7f57**","attention":""}}]]></data><respDesc>\u6210\u529f\uff01</respDesc><sign>3CE97EF5D3BB533B095D05847CB91A82</sign><transLogNo>000296</transLogNo><transDate>20140329</transDate><dataType>json</dataType><transTime>223208</transTime></JFPay>'
def info(mobile_no):
    shanghai = timezone('Asia/Shanghai')
    sa_time = datetime.datetime.now(shanghai)
    transDate = sa_time.strftime("%Y%m%d")
    transTime = sa_time.strftime("%H%M%S")
    transLogNo = "000296"
    
    #sign = r"%3CJFPay+application%3D%22UserInfoQuery.Req%22+version%3D%222.5.3%22+osType%3D%22iOS7.1%22+clientType%3D%2204%22+appUser%3D%22jfpal%22+userIP%3D%22192.168.0.105%22+mobileSerialNum%3D%22B8%3AE8%3A56%3A41%3A84%3AFC%22+phone%3D%220000%22+token%3D%220000%22%3E%3CmobileNo%3E"+ mobile_no +"%3C%2FmobileNo%3E%3CtransLogNo%3E000290%3C%2FtransLogNo%3E%3CtransTime%3E"+ transTime +"%3C%2FtransTime%3E%3CtransDate%3E"+ transDate +"%3C%2FtransDate%3E%3CtransType%3E01%3C%2FtransType%3E%3Csign%3E36f9c59d46ad450f84c47f81e10b429c%3C%2Fsign%3E%3C%2FJFPay%3E";
    sign = r"%3CJFPay+application%3D%22UserInfoQuery.Req%22+version%3D%222.5.3%22+osType%3D%22iOS7.1%22+clientType%3D%2204%22+appUser%3D%22jfpal%22+userIP%3D%22192.168.0.105%22+mobileSerialNum%3D%22B8%3AE8%3A56%3A41%3A84%3AFC%22+phone%3D%220000%22+token%3D%220000%22%3E%3CmobileNo%3E"+ mobile_no +"%3C%2FmobileNo%3E%3CtransLogNo%3E"+transLogNo+"%3C%2FtransLogNo%3E%3CtransTime%3E"+ transTime +"%3C%2FtransTime%3E%3CtransDate%3E"+ transDate +"%3C%2FtransDate%3E%3CtransType%3E01%3C%2FtransType%3E%3Csign%3E36f9c59d46ad450f84c47f81e10b429c%3C%2Fsign%3E%3C%2FJFPay%3E"
    mSign = hashlib.md5(sign).hexdigest().upper()
    
    #requestXml = r'<JFPay application="UserInfoQuery.Req" version="2.5.3" osType="iOS7.1" clientType="04" appUser="jfpal" userIP="192.168.0.105" mobileSerialNum="B8:E8:56:41:84:FC" phone="0000" token="0000"><mobileNo>%s</mobileNo><transLogNo>000290</transLogNo><transDate>%s</transDate><transTime>%s</transTime><transType>01</transType><sign>%s</sign></JFPay>' % (mobile_no, transDate, transTime, mSign)
    requestXml = r'<JFPay application="UserInfoQuery.Req" version="2.5.3" osType="iOS7.1" clientType="04" appUser="jfpal" userIP="192.168.0.105" mobileSerialNum="B8:E8:56:41:84:FC" phone="0000" token="0000"><mobileNo>' + mobile_no + '</mobileNo><transLogNo>'+transLogNo+'</transLogNo><transTime>'+transTime+'</transTime><transDate>'+transDate+'</transDate><transType>01</transType><sign>'+mSign+'</sign></JFPay>'
    #print requestXml
    
    #mSign = md5.md5(sign).hexdigest().upper()
    r = requests.post('https://mfront.jfpal.com/unifiedAction.do', verify=False, data={'requestXml':requestXml}, timeout=1.0)
    
    #print transDate, transTime, r.text
    startHandle = '"realName":"'
    start = r.text.find(startHandle)
    
    if start == -1:
        return None
    
    start += len(startHandle)
    
    end = r.text.find('",', start)
    return r.text[start:end]

def validToken(mobile_no, token):
    #print mobile_no, token
    #print "info", info(mobile_no)
    #return True
    if info(mobile_no):
        return True
    return False


if __name__ == "__main__":
    info("18621509910")
